﻿#include "BrowserHelper.h"

using namespace Microsoft::Win32;

namespace Client::Utils
{

    void BrowserHelper::OpenChrometBrowser(const std::string &url)
    {
        try
        {
            auto openKey = R"(SOFTWARE\Wow6432Node\Google\Chrome)";
            if (IntPtr::Size == 4)
            {
                openKey = R"(SOFTWARE\Google\Chrome)";
            }
            RegistryKey *appPath = Registry::LocalMachine->OpenSubKey(openKey);

            if (appPath != nullptr)
            {
                auto result = Process::Start("chrome.exe", url);
                if (result == nullptr)
                {
                    OpenIetBrowser(url);
                }
            }
            else
            {
                OpenDefaultBrowser(url);
            }
        }
        catch (...)
        {
            OpenDefaultBrowser(url);
        }
    }

    void BrowserHelper::OpenIetBrowser(const std::string &url)
    {
        try
        {
            Process::Start("iexplore.exe", url);
        }
        catch (const std::runtime_error &ex)
        {
            MessageBox::Show(ex.what());

            try
            {
                if (FileSystem::fileExists(R"(C:\Program Files\Internet Explorer\iexplore.exe)"))
                {
                    ProcessStartInfo *processStartInfo = new ProcessStartInfo();
                    processStartInfo->FileName = R"(C:\Program Files\Internet Explorer\iexplore.exe)";
                    processStartInfo->Arguments = url;
                    processStartInfo->UseShellExecute = false;
                    processStartInfo->CreateNoWindow = true;
                    Process::Start(processStartInfo);

//C# TO C++ CONVERTER TODO TASK: A 'delete processStartInfo' statement was not added since processStartInfo was passed to a method or constructor. Handle memory management manually.
                }
                else
                {
                    if (FileSystem::fileExists(R"(C:\Program Files (x86)\Internet Explorer\iexplore.exe)"))
                    {
                        ProcessStartInfo *processStartInfo = new ProcessStartInfo();
                        processStartInfo->FileName = R"(C:\Program Files (x86)\Internet Explorer\iexplore.exe)";
                        processStartInfo->Arguments = url;
                        processStartInfo->UseShellExecute = false;
                        processStartInfo->CreateNoWindow = true;
                        Process::Start(processStartInfo);

//C# TO C++ CONVERTER TODO TASK: A 'delete processStartInfo' statement was not added since processStartInfo was passed to a method or constructor. Handle memory management manually.
                    }
                    else
                    {
                        if (MessageBox::Show("Internet Explorer is not installed in the system. Do you want to download and install it?", nullptr, MessageBoxButtons::YesNoCancel, MessageBoxIcon::Question) == DialogResult::Yes)
                        {
                            OpenDefaultBrowser("http://windows.microsoft.com/zh-cn/internet-explorer/download-ie");
                        }
                    }
                }
            }
            catch (const std::runtime_error &exception)
            {
                MessageBox::Show(exception.what());
            }
        }
    }

    void BrowserHelper::OpenDefaultBrowser(const std::string &url)
    {
        try
        {
            RegistryKey *key = Registry::ClassesRoot->OpenSubKey(R"(http\shell\open\command\)");
            if (key != nullptr)
            {
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
                std::string s = key->GetValue("").ToString();
                auto lastIndex = (int)s.find(".exe", StringComparison::Ordinal);
                auto path = s.substr(1, lastIndex + 3);
                auto result = Process::Start(path, url);
                if (result == nullptr)
                {
                    auto result1 = Process::Start("explorer.exe", url);
                    if (result1 == nullptr)
                    {
                        Process::Start(url);
                    }
                }
            }
            else
            {
                auto result1 = Process::Start("explorer.exe", url);
                if (result1 == nullptr)
                {
                    Process::Start(url);
                }
            }
        }
        catch (...)
        {
            OpenIetBrowser(url);
        }
    }
}
